<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/7/16
 * Time: 13:59
 * 短视频
 */
namespace app\api\controller;
use think\Db;
class Video extends Base {
    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == 112){
            echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
            exit;
        }elseif($auth == 110){
            echo json_encode(['status' => 302, 'msg' => $this->frost]);
            exit;
        }elseif($auth == 2111){
            echo json_encode(['status' => 303, 'msg' => $this->del]);
            exit;
        }
    }

    //视频分类
    public function videoType(){
        $where['video_type_status'] = 1;
        $res = Db::table('up_video_type')->where($where)->select();
        return  json(array('status'=>200,'msg'=>$res));
    }

    //短视频列表
    public function index(){
        $video_type_id = input('param.video_type_id');
        $page = input('param.page')?input('param.page'):1;
        $num = 10;
        $limit = $page-1;
        if($video_type_id==''){
            $videoType = Db::table('up_video_type')->find();
            $where['video_type_id'] = $videoType['video_type_id'];
        }else{
            $where['video_type_id'] = $video_type_id;
        }
        $where['video_status']=2;
        $res = Db::table('up_video')->alias('a')
             ->join('up_u_user uuu','uuu.user_id = a.video_user_id','left')
             ->where($where)
             ->field('uuu.user_id,uuu.user_username,uuu.user_image,a.video_id,a.video_url')
             ->limit($limit*$num,$num)
             ->order('video_time desc')
             ->select();
        return  json(array('status'=>200,'msg'=>$res));
    }

    //发布短视频
    public function addVideo(){
        extract(input());
        $video_type_id = input('param.video_type_id');
        $video_title = input('param.video_title');
        $video_title = $this->sensitive($video_title);
        $video_url = input('param.video_url');
        $data['video_title'] = $video_title;
        $data['video_url'] = $video_url;
        $data['video_type_id'] = $video_type_id;
        $data['video_user_id'] = $user_id;
        $data['video_time'] = date('Y-m-d H:i:s',time());
        $res = Db::table('up_video')->insertGetId($data);
        if($res){
            Db::table('up_tishi_admin')->where('id', 1)->setField('video', 2); // 用于后台展示小红点
            return  json(array('status'=>200,'msg'=>"发布成功"));
        }else{
            return  json(array('status'=>200,'msg'=>"发布失败"));
        }
    }
    
    /**
     * 上滑/下滑
     * @return \think\response\Json
     * get_type    1 上滑    2 下滑
     * video_id    ID
     * video_type_id    分类ID
     */
    public function getOneVideo(){
        $get_type = input('get_type'); // 1 上滑   2 下滑
        $video_id = input('video_id');
        $video_type_id = input('video_type_id');
        if($get_type == 1){
            $res = Db::table('up_video')
                ->field('video_id, video_url')
                ->where('video_id', '<', $video_id)
                ->where('video_type_id', $video_type_id)
                ->where('video_status', 2)
                ->order('video_id desc')
                ->find();
            if($res){
                return json(['status' => 200, 'msg' => $res]);
            }else{
                return json(['status' => 0, 'msg' => '暂无数据']);
            }
        }else{
            $res = Db::table('up_video')
                ->field('video_id, video_url')
                ->where('video_id', '>', $video_id)
                ->where('video_type_id', $video_type_id)
                ->where('video_status', 2)
                ->order('video_id asc')
                ->find();
            if($res){
                return json(['status' => 200, 'msg' => $res]);
            }else{
                return json(['status' => 0, 'msg' => '暂无数据']);
            }
        }
    }

    //视频详情
    public function videoDetail(){
        extract(input());
        $id = input('param.video_id');
        $where['video_id'] = $id;
        $res = Db::table('up_video')->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.video_user_id','left')
            ->where($where)
            ->field('uuu.user_username,uuu.user_image,a.*')
            ->find();
        if(!$res){
            return json(['status' => 0, 'msg' => '数据不存在']);
        }
        $map['attention_user_id'] = $user_id;
        $map['attention_video_id'] = $id;
        $info = Db::table('up_attention_video')->where($map)->find();
        if($info['attention_id']!=''){//证明关注这个用户了
            $res['is_like'] = 1;
        }else{//没有关注这个用户
            $res['is_like'] = 2;
        }
        $where1['f_id'] = $id;
        $where1['type'] = 5;
        $num = Db::table('up_comment')->alias('a')
            ->where($where1)
            ->count();
        $res['video_comment'] = $num;
        return  json(array('status'=>200,'msg'=>$res));
    }
    //评论列表
    public function commentList(){
        extract(input());
        $id = input('param.id');
        $page = input('param.page')?input('param.page'):1;
        $num = 10;
        $limit = $page-1;
        $where['f_id'] = $id;
        $where['type'] = 5;
        $res = Db::table('up_comment')->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.user_id','left')
            ->where($where)
            ->field('a.user_id,uuu.user_username,uuu.user_image,a.comment,a.comment_id,a.time')
            ->limit($limit*$num,$num)
            ->order('a.time desc')
            ->select();
        foreach ($res as $key=>&$vo){
            //评论有多少回复
            $count = Db::table('up_comment_reply')->where('f_id',$vo['comment_id'])->count();
            $vo['count'] = $count;
            //评论有多少点赞
            $map['give_f_id'] = $vo['comment_id'];
            $map['give_type'] = 1;
            $count1 = Db::table('up_comment_give')->where($map)->count();
            $vo['count1'] = $count1;

            $map1['give_type'] = 1;
            $map1['give_f_id'] = $vo['comment_id'];
            $map1['give_user_id'] = $user_id;
            $info = Db::table('up_comment_give')->where($map1)->find();
            if($info['give_id']!=''){//点赞过
               $vo['is_dz'] = 1;
            }else{
                $vo['is_dz'] = 2;
            }
        }
        //多少评论
        $num = Db::table('up_comment')->alias('a')
            ->where($where)
            ->count();
        $data['res'] = $res;
        $data['num'] = $num;
        return  json(array('status'=>200,'msg'=>$data));
    }
    //评论
    public function comment(){
        extract(input());
        $content = input('param.comment');
        $content = $this->sensitive($content);
        $video_id = input('param.video_id');
        $data['f_id'] = $video_id;
        $data['comment'] = $content;
        $data['time'] = date('Y-m-d H:i:s',time());
        $data['user_id'] = $user_id;
        $data['type'] = 5;
        $data['f_user_id'] = input('param.other_user');
        $res = Db::table('up_comment')->insertGetId($data);
        if($res){
            return  json(array('status'=>200,'msg'=>"发布成功"));
        }else{
            return  json(array('status'=>200,'msg'=>"发布失败"));
        }
    }

    //评论点赞
    public function videoDz(){
        // type  1短视频评论点赞   2短视频评论回复点赞   3短视频评论取消点赞   4 短视频评论回复取消点赞
        extract(input());
        $type = input('param.type');
        if($type==1) {
            $data['give_user_id'] = $user_id;
            $data['give_time'] = date('Y-m-d H:i:s', time());
            $data['give_type'] = 1;
            $data['give_f_id'] = input('param.id');
            $data['give_f_f_id'] = input('param.pl_id');
            $data['give_by_user_id'] = input('param.other_user');
            $res = Db::table('up_comment_give')->insertGetId($data);
            if ($res) {
                return json(array('status' => 200, 'msg' => "点赞成功"));
            } else {
                return json(array('status' => 200, 'msg' => "点赞失败"));
            }
        }elseif ($type==2){
            $data['give_user_id'] = $user_id;
            $data['give_time'] = date('Y-m-d H:i:s',time());
            $data['give_type'] = 2;
            $data['give_f_id'] = input('param.id');
            $data['give_f_f_id'] = input('param.pl_id');
            $data['give_by_user_id'] = input('param.other_user');
            $res = Db::table('up_comment_give')->insertGetId($data);
            if($res){
                return  json(array('status'=>200,'msg'=>"点赞成功"));
            }else{
                return  json(array('status'=>200,'msg'=>"点赞失败"));
            }
        }elseif ($type==3){
            $data['give_user_id'] = $user_id;
            $data['give_type'] = 1;
            $data['give_f_id'] = input('param.id');
            $res = Db::table('up_comment_give')->where($data)->delete();
            if ($res) {
                return json(array('status' => 200, 'msg' => "取消点赞成功"));
            } else {
                return json(array('status' => 200, 'msg' => "取消点赞失败"));
            }
        }elseif ($type==4){
            $data['give_user_id'] = $user_id;
            $data['give_type'] = 2;
            $data['give_f_id'] = input('param.id');
            $res = Db::table('up_comment_give')->where($data)->delete();
            if ($res) {
                return json(array('status' => 200, 'msg' => "取消点赞成功"));
            } else {
                return json(array('status' => 200, 'msg' => "取消点赞失败"));
            }
        }
    }


    //短视频评论回复列表
    public function videoAnswer(){
        extract(input());
        $comment_id = input('param.comment_id');
        $page = input('param.page')?input('param.page'):1;
        $num = 10;
        $limit = $page-1;
        $where['comment_id'] = $comment_id;
        $res = Db::table('up_comment')->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.user_id','left')
            ->where($where)
            ->field('uuu.user_username,uuu.user_image,a.comment,a.comment_id,a.user_id,a.time')
            ->find();
        $whereMap['give_f_id'] = $comment_id;
        $whereMap['give_type'] = 1;
        $whereMap['give_user_id'] = $user_id;
        $dataRes = Db::table('up_comment_give')->where($whereMap)->find();
        if($dataRes['give_id']!=''){
            $res['dz'] = 1;
        }else{
            $res['dz'] = 2;
        }
        $whereMap1['give_f_id'] = $comment_id;
        $whereMap1['give_type'] = 1;
        $count3 = Db::table('up_comment_give')->where($whereMap1)->count();
        $res['count3'] = $count3;
        $map2['a.f_id'] = $comment_id;
        $info = Db::table('up_comment_reply')->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.user_id','left')
            ->field('a.*,uuu.user_username,uuu.user_image')
            ->where($map2)
            ->limit($num*$limit,$num)
            ->select();
        foreach ($info as $key=>&$vo){
            //评论回复有多少点赞
            $map['give_f_id'] = $vo['comment_reply_id'];
            $map['give_type'] = 2;
            $count1 = Db::table('up_comment_give')->where($map)->count();
            $vo['count1'] = $count1;
            //点赞
            $map1['give_type'] = 2;
            $map1['give_f_id'] = $vo['comment_reply_id'];
            $map1['give_user_id'] = $user_id;
            $info1 = Db::table('up_comment_give')->where($map1)->find();
            if($info1['give_id']!=''){//点赞过
                $vo['is_dz'] = 1;
            }else{
                $vo['is_dz'] = 2;
            }
        }
        //多少回复
        $num = Db::table('up_comment_reply')
            ->where('f_id',$comment_id)
            ->count();
        $res['num'] = $num;
        $res['info'] = $info;
        return  json(array('status'=>200,'msg'=>$res));
    }

    //评论回复
    public function answer(){
        extract(input());
        $content = input('param.content');
        $content = $this->sensitive($content);
        $video_id = input('param.id');
        $data['f_id'] = $video_id;
        $data['comment_reply_content'] = $content;
        $data['comment_time'] = date('Y-m-d H:i:s',time());
        $data['user_id'] = $user_id;
        $data['by_user_id'] = input('param.other_user');
        $res = Db::table('up_comment_reply')->insertGetId($data);
        if($res){
            return  json(array('status'=>200,'msg'=>"发布成功"));
        }else{
            return  json(array('status'=>200,'msg'=>"发布失败"));
        }
    }

    //关注
    public function like(){
        extract(input());
        $id = input('param.id');
        $where['attention_user_id'] = $user_id;
        $where['attention_video_id'] = $id;
        $aa = Db::table('up_attention_video')->where($where)->find();
        if($aa['attention_id']!=''){//证明存在
            $res = Db::table('up_attention_video')->where($where)->delete();
        }else{
            $data['attention_time'] = date('Y-m-d H:i:s',time());
            $data['attention_user_id'] = $user_id;
            $data['attention_video_id'] = $id;
            $res = Db::table('up_attention_video')->insertGetId($data);
        }
        if ($res) {
            return json(array('status' => 200, 'msg' => "成功"));
        } else {
            return json(array('status' => 200, 'msg' => "失败"));
        }
    }
}